package com.jlastu.znb.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ByGoodsActivity extends AppCompatActivity {

    private ImageView mBack;
    private TextView mAddressTextView;
    private RelativeLayout mGoodsAddressLayout;
    private RelativeLayout mWantAddressLayout;
    private TextView mWantAddressTextView;
    private TextView mClassSelectTextView;
    private ImageView mPriceSelectImageView;
    private TextView mPriceSelectTextView;
    private ImageView mTimeSelectImageView;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caigou);


        mBack = findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                finish();
            }
        });
        mClassSelectTextView = findViewById(R.id.class_select);
        mPriceSelectTextView = findViewById(R.id.price);
        mPriceSelectImageView = findViewById(R.id.price_select);

        mAddressTextView = findViewById(R.id.tv_goods_address_hint);
        mGoodsAddressLayout = findViewById(R.id.goods_address);

        mWantAddressLayout = findViewById(R.id.want_goods_address);
        mWantAddressTextView = findViewById(R.id.cg_goods_address_hint);

        mTimeSelectImageView=findViewById(R.id.cg_time_select);






        // 设置分类选择点击事件
        mClassSelectTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClassPicker();
            }
        });
        //需求量点击事件

        mPriceSelectImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPricePicker();
            }
        });
        //期望货源地点击事件
        mWantAddressLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWantAddressPicker();
            }
        });
        //采购周期点击事件
        mTimeSelectImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker();
            }
        });
        //收购地址选择点击事件
        mGoodsAddressLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddressPicker();
            }
        });
    }



//货品类型
private void showClassPicker() {
    // 创建 AlertDialog
    final AlertDialog.Builder builder = new AlertDialog.Builder(ByGoodsActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.goods_class_select, null); // 使用您的分类选择布局
    builder.setView(dialogView);
    final AlertDialog dialog = builder.create();

    final RadioGroup radioGroup = dialogView.findViewById(R.id.radio_group);
    TextView determineButton = dialogView.findViewById(R.id.determine);
    ImageView closeButton = dialogView.findViewById(R.id.close_button);

    // 初始化时禁用确定按钮
    determineButton.setEnabled(false);

    // RadioGroup 的监听器，用于检查是否选择了一个选项
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 启用确定按钮
            determineButton.setEnabled(true);
            // 更新 mClassSelectTextView 文本
            String text = (String) ((RadioButton) group.findViewById(checkedId)).getText();
            mClassSelectTextView.setText(text);
        }
    });

    // determineButton 的点击事件
    determineButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 点击确定后的操作
            dialog.dismiss();
        }
    });

    // closeButton 的点击事件
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    // 显示对话框
    dialog.show();
}



//需求量


    private void showPricePicker() {
        // 创建 AlertDialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(ByGoodsActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.price_select, null); // 使用您的价格选择布局
        builder.setView(dialogView);
        final AlertDialog dialog = builder.create();

        // 获取 RadioGroup 和其他控件
        final RadioGroup radioGroup = dialogView.findViewById(R.id.radio_group);
        final TextView determineButton = dialogView.findViewById(R.id.determine);
        ImageView closeButton = dialogView.findViewById(R.id.close_button);
        final TextView priceTextView = findViewById(R.id.price); // 确保这个ID与您的布局文件中定义的相符

        // 为 RadioGroup 设置监听器，以便在选项变化时更新界面
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 当选择变化时，获取选中的RadioButton的文本，并更新priceTextView
                RadioButton selectedRadioButton = radioGroup.findViewById(checkedId);
                priceTextView.setText(selectedRadioButton.getText());
            }
        });

        // 为关闭按钮设置点击事件
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        // 为确定按钮设置点击事件
        determineButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确定后的操作，可以在这里添加其他逻辑，比如保存选中的单位
                dialog.dismiss();
            }
        });

        // 显示对话框
        dialog.show();
    }




    //期望货源地
    private void showWantAddressPicker () {
        // 假设这是从数据库或其他数据源获取的省市区数据
        Map<String, List<String>> provinceCityMap = new HashMap<>();
        Map<String, List<String>> cityDistrictMap = new HashMap<>();

        // 示例数据填充
        provinceCityMap.put("吉林省", Arrays.asList("长春市", "吉林市", "四平市"));
        provinceCityMap.put("云南省", Arrays.asList("昆明市", "曲靖市", "玉溪市"));

        // 假设每个城市下都有一些区域
        cityDistrictMap.put("长春市", Arrays.asList("南关区", "宽城区", "朝阳区"));
        cityDistrictMap.put("吉林市", Arrays.asList("昌邑区", "龙潭区", "船营区"));
        cityDistrictMap.put("昆明市", Arrays.asList("五华区", "盘龙区", "西山区"));
        cityDistrictMap.put("曲靖市", Arrays.asList("麒麟区", "沾益区", "马龙区"));

        final AlertDialog.Builder builder = new AlertDialog.Builder(ByGoodsActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.address_select, null);
        builder.setView(dialogView);
        final AlertDialog dialog = builder.create();

        // 初始化 Spinner
        final Spinner spinnerProvince = dialogView.findViewById(R.id.spinner_province);
        final Spinner spinnerCity = dialogView.findViewById(R.id.spinner_city);
        final Spinner spinnerDistrict = dialogView.findViewById(R.id.spinner_district);

        // 填充省份 Spinner
        ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, new ArrayList<>(provinceCityMap.keySet()));
        provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProvince.setAdapter(provincesAdapter);

        // 省份 Spinner 监听器
        spinnerProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedProvince = parent.getItemAtPosition(position).toString();
                List<String> cities = provinceCityMap.get(selectedProvince);
                // 更新城市 Spinner
                ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(
                        ByGoodsActivity.this,
                        android.R.layout.simple_spinner_item, cities);
                citiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerCity.setAdapter(citiesAdapter);
                // 重置区域 Spinner
                spinnerDistrict.setAdapter(new ArrayAdapter<>(
                        ByGoodsActivity.this,
                        android.R.layout.simple_spinner_item, new ArrayList<>()));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing
            }
        });

        // 填充城市 Spinner
        ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, new ArrayList<>(provinceCityMap.values().iterator().next()));
        citiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCity.setAdapter(citiesAdapter);

        // 城市 Spinner 监听器
        spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCity = parent.getItemAtPosition(position).toString();
                List<String> districts = cityDistrictMap.get(selectedCity);
                // 更新区域 Spinner
                ArrayAdapter<String> districtsAdapter = new ArrayAdapter<>(
                        ByGoodsActivity.this,
                        android.R.layout.simple_spinner_item, districts);
                districtsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerDistrict.setAdapter(districtsAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing
            }
        });

        // 设置对话框按钮
        TextView determineButton = dialogView.findViewById(R.id.determine);
        determineButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取选中的省市区
                String selectedProvince = spinnerProvince.getSelectedItem().toString();
                String selectedCity = spinnerCity.getSelectedItem().toString();
                String selectedDistrict = spinnerDistrict.getSelectedItem().toString();

                // 构建完整的地址字符串
                String fullAddress = selectedProvince + " " + selectedCity + " " + selectedDistrict;

                // 更新发货地址文本
                mWantAddressTextView.setText(fullAddress);

                // 关闭对话框
                dialog.dismiss();
            }
        });
        ImageView closeButton = dialogView.findViewById(R.id.close_button);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss(); // 关闭对话框
            }
        });

        // 显示对话框
        dialog.show();
    }





    //采购周期
    private void showTimePicker() {
        // 创建 AlertDialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(ByGoodsActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.time_select, null); // 使用您的价格选择布局
        builder.setView(dialogView);
        final AlertDialog dialog = builder.create();

        // 获取 RadioGroup 和其他控件
        final RadioGroup radioGroup = dialogView.findViewById(R.id.radio_group);
        final TextView determineButton = dialogView.findViewById(R.id.determine);
        ImageView closeButton = dialogView.findViewById(R.id.close_button);
        final TextView timeTextView = findViewById(R.id.time); // 确保这个ID与您的布局文件中定义的相符

        // 为 RadioGroup 设置监听器，以便在选项变化时更新界面
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 当选择变化时，获取选中的RadioButton的文本，并更新priceTextView
                RadioButton selectedRadioButton = radioGroup.findViewById(checkedId);
                timeTextView.setText(selectedRadioButton.getText());
            }
        });

        // 为关闭按钮设置点击事件
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        // 为确定按钮设置点击事件
        determineButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确定后的操作，可以在这里添加其他逻辑，比如保存选中的单位
                dialog.dismiss();
            }
        });

        // 显示对话框
        dialog.show();
    }





    //收购地址
        private void showAddressPicker () {
            // 假设这是从数据库或其他数据源获取的省市区数据
            Map<String, List<String>> provinceCityMap = new HashMap<>();
            Map<String, List<String>> cityDistrictMap = new HashMap<>();

            // 示例数据填充
            provinceCityMap.put("吉林省", Arrays.asList("长春市", "吉林市", "四平市"));
            provinceCityMap.put("云南省", Arrays.asList("昆明市", "曲靖市", "玉溪市"));

            // 假设每个城市下都有一些区域
            cityDistrictMap.put("长春市", Arrays.asList("南关区", "宽城区", "朝阳区"));
            cityDistrictMap.put("吉林市", Arrays.asList("昌邑区", "龙潭区", "船营区"));
            cityDistrictMap.put("昆明市", Arrays.asList("五华区", "盘龙区", "西山区"));
            cityDistrictMap.put("曲靖市", Arrays.asList("麒麟区", "沾益区", "马龙区"));

            final AlertDialog.Builder builder = new AlertDialog.Builder(ByGoodsActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.address_select, null);
            builder.setView(dialogView);
            final AlertDialog dialog = builder.create();

            // 初始化 Spinner
            final Spinner spinnerProvince = dialogView.findViewById(R.id.spinner_province);
            final Spinner spinnerCity = dialogView.findViewById(R.id.spinner_city);
            final Spinner spinnerDistrict = dialogView.findViewById(R.id.spinner_district);

            // 填充省份 Spinner
            ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_spinner_item, new ArrayList<>(provinceCityMap.keySet()));
            provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerProvince.setAdapter(provincesAdapter);

            // 省份 Spinner 监听器
            spinnerProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String selectedProvince = parent.getItemAtPosition(position).toString();
                    List<String> cities = provinceCityMap.get(selectedProvince);
                    // 更新城市 Spinner
                    ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(
                            ByGoodsActivity.this,
                            android.R.layout.simple_spinner_item, cities);
                    citiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinnerCity.setAdapter(citiesAdapter);
                    // 重置区域 Spinner
                    spinnerDistrict.setAdapter(new ArrayAdapter<>(
                            ByGoodsActivity.this,
                            android.R.layout.simple_spinner_item, new ArrayList<>()));
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // Do nothing
                }
            });

            // 填充城市 Spinner
            ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_spinner_item, new ArrayList<>(provinceCityMap.values().iterator().next()));
            citiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerCity.setAdapter(citiesAdapter);

            // 城市 Spinner 监听器
            spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String selectedCity = parent.getItemAtPosition(position).toString();
                    List<String> districts = cityDistrictMap.get(selectedCity);
                    // 更新区域 Spinner
                    ArrayAdapter<String> districtsAdapter = new ArrayAdapter<>(
                            ByGoodsActivity.this,
                            android.R.layout.simple_spinner_item, districts);
                    districtsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinnerDistrict.setAdapter(districtsAdapter);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // Do nothing
                }
            });

            // 设置对话框按钮
            TextView determineButton = dialogView.findViewById(R.id.determine);
            determineButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取选中的省市区
                    String selectedProvince = spinnerProvince.getSelectedItem().toString();
                    String selectedCity = spinnerCity.getSelectedItem().toString();
                    String selectedDistrict = spinnerDistrict.getSelectedItem().toString();

                    // 构建完整的地址字符串
                    String fullAddress = selectedProvince + " " + selectedCity + " " + selectedDistrict;

                    // 更新发货地址文本
                    mAddressTextView.setText(fullAddress);

                    // 关闭对话框
                    dialog.dismiss();
                }
            });
            ImageView closeButton = dialogView.findViewById(R.id.close_button);
            closeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss(); // 关闭对话框
                }
            });

            // 显示对话框
            dialog.show();


        }}












